﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace SourceMaster.Util
{
    public class DelegateCommand : ICommand
    {
        private readonly Action<object> execute;
        private readonly Predicate<object> canExecute;

        public DelegateCommand(Action<object> execute, Predicate<object> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");
            this.execute = execute;
            this.canExecute = canExecute;
        }

        public DelegateCommand(Action<object> execute) : this(execute, null) { }

        event EventHandler ICommand.CanExecuteChanged
        {
            add
            {
                CommandManager.RequerySuggested += value;
                //throw new NotImplementedException();
            }

            remove
            {
                CommandManager.RequerySuggested -= value;
                //throw new NotImplementedException();
            }
        }

        bool ICommand.CanExecute(object parameter)
        {
            //throw new NotImplementedException();
            return canExecute == null ? true : canExecute(parameter);
        }

        void ICommand.Execute(object parameter)
        {
            //throw new NotImplementedException();
            execute(parameter);
        }
    }
}
